/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import util.HibernateUtil;
import java.util.List;
import pojo.Danhmuc;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Jackhit
 */
public class DanhMucDAO {
    public static List<Danhmuc> layDanhSachDanhMuc() {
        List<Danhmuc> dsDanhMuc = null;
         Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            String hql = "from Danhmuc";
            Query query = session.createQuery(hql);
            dsDanhMuc = query.list();
         }catch(Exception ex) {
            System.out.println(ex.getMessage());
        }finally {
            session.close();
        }
        return dsDanhMuc;
    }

    public static boolean themDanhMuc(Danhmuc danhMuc) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(danhMuc);
            session.getTransaction().commit();
            return true;
        }catch(Exception e) {
            session.getTransaction().rollback();
            return false;
        }
        finally {
            session.close();
        }
    }

    public static boolean capNhatDanhMuc(Danhmuc danhMuc) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(danhMuc);
            session.getTransaction().commit();
            return true;
        }catch(Exception e) {
            session.getTransaction().rollback();
            return false;
        }
        finally {
            session.close();
        }
    }

    public static boolean xoaDanhMuc(Danhmuc danhMuc) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(danhMuc);
            session.getTransaction().commit();
            return true;
        }catch(Exception e) {
            session.getTransaction().rollback();
            return false;
        }
        finally {
            session.close();
        }
    }
}
